<template>
	<view class="layout">
		<view>
			<view class="header-top">

			</view>
			<view class="header-top acea-row row-between-wrapper row-middle">
				<view>
					<text class="iconfont icon-fanhui2 px-20" style="color: #fff;" @tap="returns"></text>
				</view>
				<view class="header-top-tittle">我的发布-供应链</view>
				<view @click="toPage('/pages/releases/supplyChain/supplyChain')">
					<img src="../static/images/add.png" style='width: 40rpx; height: 40rpx;margin-top: 15rpx;'>
				</view>
			</view>
			<view class="acea-row row-middle row-between nav-bg">
				<scroll-view scroll-x="true" style="white-space: nowrap; display: flex;" scroll-with-animation
					:scroll-left="tabLeft" show-scrollbar="true">
					<view class="item" :style='"width:"+isWidth+"px"' :data-index="index"
						:class="[index===categoryOn?'item-on':'',index>4?'item-mr-l':'']"
						v-for="(item,index) in categoryList" :key="index" :id="'id'+index"
						@click="categoryNavClick(item,index)">
						<view class="acea-row row-middle row-center img">
							<image :src="item.icon" mode=""></image>
						</view>
						<view class="name">
							{{item.name}}
						</view>
					</view>
				</scroll-view>
			</view>
			<view class="sort-box" style="margin-bottom: 20rpx;">
				<scroll-view scroll-x="true" style="white-space: nowrap; display: flex;">
					<template v-for="(item,index) in sortList">
						<view :key="index" :class="['sort',sortOn==index?'sort-on':'']"
							@click="sortClick(index,item.status)">{{item.name}}</view>
					</template>
				</scroll-view>
			</view>
		</view>
		<view style="flex: 1;overflow: auto;">
			<block v-for="(item,index) in dataList" :key="index">
				<supply :item="item" @to-topping="toTopping" @setMeth="setMeth" @toVip="toVip" @visitor="toVisitor" @updateItem="((k,v)=>$set(dataList[index],k,v))"/>
				<!-- 			<askRent v-else :item="item" /> -->
			</block>
			<view class='loadingicon acea-row row-center row-middle'>
				<text class='loading iconfont icon-jiazai' :hidden='loading'></text>{{loadTitle}}
			</view>
			<view class="bottom acea-row row-center" @click="onRefresh">
				一键刷新
			</view>
		</view>

		<topping :show="linkShow" :link-id="linkId" :types="linkTypes" @close="linkShow=false" @opear="toOpear" />
		<setting ref="setting" :show="setShow" @close="setShow=false" :item="settingItem" @opear="toOpear"
			@toChange="settingchange" />
	</view>
</template>

<script>
	import supply from "../components/supply.vue"

	import {
		AllListApi,
		OnListApi,
		getOnGroupListApi,
		BuyListApi
	} from '@/api/subletOfShop.js'
	import {
		getclassificationListApi
	} from '@/api/brand/index.js'
	import {
		getSuppleOnList,
		getSuppleBegOnList
	} from "@/api/api.js";
	import {
		getCategory,
		toPage,
		showLoading
	} from '@/filters/commFilter.js'
	import topping from '../components/topping.vue'
	import setting from '../components/setting.vue'
	export default {
		components: {
			supply,
			topping,
			setting
		},
		data() {
			return {
				tabRight: 0,
				widthIng: 60,
				isWidth: 75,
				isLeft: 0,
				tabLeft: 0,
				typeOn: 0,
				categoryList: [],
				categoryOn: 0,
				dataList: [],
				page: 1,
				loadTitle: '',
				loading: true,
				linkShow: false,
				linkId: 0,
				linkTypes: '',
				setShow: false,
				settingItem: {},
				sortOn: 0,
				sortList: [{
					name: '供应链',
					status: 0
				}, {
					name: '求供应链',
					status: 1
				}],
			}
		},
		created() {

			this.getSupplyList()
		},
		onReachBottom() {

		},
		methods: {
			toPage,
			toTopping(linkId, linkType) {
				this.linkId = linkId;
				this.linkTypes = linkType;
				this.linkShow = true;
			},
			setMeth(data) {
				this.settingItem = data;
				console.log("setData", data)
				this.setShow = true
			},
			settingchange(k, v) {
				this.settingItem[k] = v;
				// this.$set(this.settingItem,k,v)
				this.dataList.forEach(item => {
					if ((item.id && item.id == this.settingItem.id) || (item.askrentId && item.askrentId == this
							.settingItem.askrentId) || (item.buystoreId && item.buystoreId == this.settingItem
							.buystoreId)) {
						console.log('sdfsfsf')
						item[k] = this.settingItem[k]
					}
				})
			},
			sortClick(index, status) {
				this.sortOn = index;
				this.page = 1;
				this.getList();
			},
			toOpear(message) {
				this.$util.Tips({
					title: message
				})
				this.page = 1;
				this.getList();
			},
			//获取数据列表
			async getList() {
				this.loading = false;
				this.loadTitle = '加载中'
				let t = this.sortList[this.sortOn].status
				try {
					let res = null;
					if (t == 0) {
						res = await getSuppleOnList({
							page: this.page,
							category: this.categoryList[this.categoryOn].id,
						});
					}else{
						res = await getSuppleBegOnList({
							page: this.page,
							category: this.categoryList[this.categoryOn].id,
						});
					}


					this.loading = true
					if (this.page == 1) {
						this.dataList = res.data.list ? res.data.list.map(item => {
							item.imaegs = item.imaegs ? item.imaegs : this.$noImageShow;
							if(item.demandStatus == 1){
								item.vistype = 11;
							}else{
								item.vistype = 4;
							}
							
							return item;
						}) : [];
						if (this.dataList.length <= 0) {
							this.loadTitle = '暂无数据'
						} else {
							this.loadTitle = ''
						}
					} else {
						this.dataList = this.dataList.concat(res.data.list);
						this.dataList = res.data.list ? res.data.list.map(item => {
							item.imaegs = item.imaegs ? item.imaegs : this.$noImageShow;
							if(item.demandStatus == 1){
								item.vistype = 11;
							}else{
								item.vistype = 4;
							}
							
							return item;
						}) : [];
						if (res.data.list.length <= 0) {
							this.loadTitle = '已加载所有'
						} else {
							this.loadTitle = '加载更多'
						}
					}
					uni.hideLoading()
				} catch (e) {
					console.log(e)
					//TODO handle the exception
				}
			},
			toVip(data) {
				this.settingItem = data;
				console.log("this.settingItem", this.settingItem)
				let that = this;
				let ti = data.isVip && data.isVip == 1 ? '取消' : '开启';
				let st = data.isVip && data.isVip == 1 ? 0 : 1;
				uni.showModal({
					title: '提示',
					content: '是否' + ti + '信息VIP',
					success(res) {
						if (res.confirm) {
							that.$refs.setting.settingVip(st)
						}
					}
				})
			},
			toVisitor(data) {
				uni.navigateTo({
					url: "/pages/aboutUsers/aboutUsers/visitor?mind=1&data=" + JSON.stringify(data)
				})
			},
			//获取供应链
			async getSupplyList() {
				showLoading()

				try {
					let res = await getclassificationListApi({
						status: 1
					});
					uni.hideLoading()
					this.categoryList = res.data;
					this.getList()

				} catch (e) {

				} finally {

				}
			},
			typeNavClick(item, index) {
				// let title = '发布-'+this.categoryList[this.categoryOn].name+item.name
				// uni.setNavigationBarTitle({
				// 	title:title
				// })
				this.typeOn = index
				if (this.types.length > 5) {
					var tempIndex = index - 1;
					tempIndex = tempIndex <= 0 ? 0 : tempIndex;
					this.tabRight = (index - 2) * this.widthIng
				}
				this.initList()
			},
			// 导航栏点击
			categoryNavClick(item, index) {
				if (this.categoryList.length > 6) {
					var tempIndex = index - 2;
					tempIndex = tempIndex <= 0 ? 0 : tempIndex;
					this.tabLeft = (index - 2) * this.isWidth //设置下划线位置
				}
				this.categoryOn = index //设置导航点击了哪一个
				this.isLeft = index * this.isWidth //设置下划线位置
				this.getSupplyList();
			},
			//刷新
			onRefresh() {
				this.categoryOn = 0;
				this.typeOn = 0;
				this.getList()
			},
			returns: function() {
				uni.navigateBack();
			},
		}
	}
</script>

<style lang="scss">
	.layout {
		display: flex;
		flex-direction: column;
		height: 100vh;

		.scroll {
			overflow-y: auto;
			padding-bottom: 35rpx;
			flex: 1;

			.scroll-Y {
				height: 100%;
			}
		}

	}

	.header-top {
		height: 88rpx;
		background: rgba(238, 33, 45, 1);
		padding: 0rpx 30rpx;

		.header-top-tittle {
			line-height: 0rpx;
			font-size: 36rpx;
			color: rgba(255, 255, 255, 1);
		}
	}

	.nav-bg {
		background: #ffffff;
		padding: 18rpx 30rpx 36rpx;

		.item~.item {
			// margin-left: 48rpx;
		}

		.item {
			display: inline-block;
			white-space: nowrap;

			.img {
				width: 80rpx;
				height: 80rpx;
				border-radius: 50%;
				background: #ffffff;

				image {
					width: 100%;
					height: 100%;
				}
			}

			.name {
				width: 70rpx;
				font-size: 26rpx;
				font-weight: 400;
				line-height: 40rpx;
				color: rgba(0, 0, 0, 1);
				text-align: center;
			}
		}

		.item-on {
			.img {
				background: rgba(247, 247, 247, 1);
			}

			.name {
				color: rgba(239, 39, 50, 1);
			}
		}
	}

	.type-bg {
		padding: 24rpx 30rpx;
		background: rgba(247, 248, 250, 1);

		.type~.type {
			margin-left: 22rpx;
		}

		.type {
			display: inline-block;
			white-space: nowrap;
			padding: 6rpx 24rpx;
			border-radius: 10rpx;
			border: 1px solid rgba(229, 229, 229, 1);
			font-size: 32rpx;
			font-weight: 400;
			color: rgba(128, 128, 128, 1);
		}

		.type-on {
			border: 1px solid rgba(238, 33, 45, 1);
			background: rgba(238, 33, 45, 1);
			color: #ffffff;

		}
	}

	.sort-box {
		display: flex;
		width: 100%;
		padding: 0rpx 30rpx 18rpx;
		background: #ffffff;

		.sort {
			display: inline-block;
			white-space: nowrap;
			border-radius: 10rpx;
			background: rgba(247, 247, 247, 1);
			padding: 12rpx 36rpx;
			font-size: 26rpx;
			font-weight: 400;
			color: rgba(50, 50, 51, 1);
			margin-right: 18rpx;
		}

		.sort-on {
			color: #ffffff;
			background: rgba(238, 33, 45, 1);
		}
	}

	.bottom {
		width: 412rpx;
		border-radius: 100px;
		background: rgba(238, 33, 45, 1);
		box-shadow: 0rpx 4rpx 20rpx 10rpx rgba(0, 0, 0, 0.05);
		line-height: 84rpx;
		font-size: 36rpx;
		color: rgba(255, 255, 255, 1);
		margin: 0 auto;
	}
</style>